local assets =
{
    Asset("ANIM", "anim/tf_cowboy_ribs.zip"),
}
-------------------------------------------------------------------------------------------------------------------

-- 学习方便，把Remap函数复制过来
-- function Remap(i, a, b, x, y)
--     return (((i - a)/(b - a)) * (y - x)) + x
-- end

local function UpdateDamage(inst)                                  --伤害变化函数
    if inst.components.perishable and inst.components.weapon then  --如果该对象有腐烂组件和武器组件
        local dmg = 78.5 * inst.components.perishable:GetPercent() --新鲜度百分比
        dmg = Remap(dmg, 0, 78.5, 43, 78.5)                        --伤害保底43，最高78.5
        inst.components.weapon:SetDamage(dmg)                      --设置dmg为伤害
    end
end

-------------------------------------------------------------------------------------------------------------------

local function OnLoad(inst, data)
    UpdateDamage(inst)
end

local function onequip(inst, owner)
    UpdateDamage(inst)                                                                     --同上：伤害变化函数
    owner.AnimState:OverrideSymbol("swap_object", "tf_cowboy_ribs", "swap_tf_cowboy_ribs") --拥有者的动画状态：手持对象是swap_cowboy_ribs
    owner.AnimState:Show("ARM_carry")                                                      --装备时显示手持动画
    owner.AnimState:Hide("ARM_normal")                                                     --不装备时隐藏手持动画
end

local function onunequip(inst, owner)
    UpdateDamage(inst)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    local skin_build = inst:GetSkinBuild()
    if skin_build ~= nil then
        owner:PushEvent("unequipskinneditem", inst:GetSkinName())
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst) --添加物理属性，使其可以被拾取等交互

    inst.AnimState:SetBank("tf_cowboy_ribs")
    inst.AnimState:SetBuild("tf_cowboy_ribs")
    inst.AnimState:PlayAnimation("idle")

    inst:AddTag("show_spoilage") --显示新鲜度，猜的
    inst:AddTag("icebox_valid")  --可以被放入冰箱

    --weapon (from weapon component) added to pristine state for optimization
    inst:AddTag("weapon")

    local floater_swap_data =
    {
        sym_build = "tf_cowboy_ribs",
        sym_name = "swap_tf_cowboy_ribs",
        bank = "tf_cowboy_ribs",
        anim = "idle"
    }
    MakeInventoryFloatable(inst, "med", 0.1, { 1.1, 0.5, 1.1 }, true, -9, floater_swap_data)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("perishable")                                 --可腐烂组件
    inst.components.perishable:SetPerishTime(14400)                 --腐烂时间
    inst.components.perishable:StartPerishing()                     --开始腐烂
    inst.components.perishable.onperishreplacement = "spoiled_food" --腐烂后变成腐烂食物

    inst:AddComponent("weapon")                                     --武器组件
    inst.components.weapon:SetDamage(78.5)
    inst.components.weapon:SetOnAttack(UpdateDamage)

    local planardamage = inst:AddComponent("planardamage")
    planardamage:SetBaseDamage(10)

    inst:AddComponent("forcecompostable")
    inst.components.forcecompostable.green = true

    inst.OnLoad = OnLoad

    inst:AddComponent("inspectable")   --可检查

    inst:AddComponent("inventoryitem") --物品栏

    MakeHauntableLaunchAndPerish(inst)

    inst:AddComponent("equippable") --可装备组件
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    return inst
end

return Prefab("tf_cowboy_ribs", fn, assets)
